import * as types from './mutation-types'
import {robotSnapShoot, ModelList} from '../api/api'
import {timestampToTime} from 'common/js/util'

export const test = function ({commit}, data) {
  commit(types.TEST, data)
}

export const acSnapShoot = function ({commit}, {param}) {
  robotSnapShoot(param).then(res => {
    // console.log(res)
    let resArr = res.data.asset_snap_shoot || []
    if (resArr.length === 0) {
      commit(types.SET_SNAP_SHOOT_LINE, {line1: [], line2: [], lineY: [], name1: '', name2: ''})
      return
    }
    let line1 = []
    let line2 = []
    let lineY = []
    let arr1 = []
    let arr2 = []
    let arr3 = []
    let arr4 = []
    let arr5 = []
    let arr6 = []
    // console.log(resArr)
    resArr.forEach((val) => {
      let baseAssetAvailable = parseFloat(val.base_asset_available)
      let baseAssetFreeze = parseFloat(val.base_asset_freeze)
      let quoteAssetAvailable = parseFloat(val.quote_asset_available)
      let quoteAssetFreeze = parseFloat(val.quote_asset_freeze)
      arr1.push(baseAssetAvailable)
      arr2.push(baseAssetFreeze)
      arr3.push(baseAssetAvailable + baseAssetFreeze)
      arr4.push(quoteAssetAvailable)
      arr5.push(quoteAssetFreeze)
      arr6.push(quoteAssetAvailable + quoteAssetFreeze)
      if (param.type === 'day') {
        lineY.push(timestampToTime(val.create_time, 3))
      } else {
        lineY.push(timestampToTime(val.create_time, 4))
      }
    })
    let arr = [arr1, arr2, arr3, arr4, arr5, arr6]
    for (let i = 0; i < 3; i++) {
      let l1 = {}
      let l2 = {}
      if (localStorage.getItem('lang') === 'en') {
        l1.name = l2.name = i === 0 ? 'Available' : (i === 1 ? 'Frozen' : 'Total')
      } else {
        l1.name = l2.name = i === 0 ? '可用' : (i === 1 ? '冻结' : '总量')
      }
      l1.type = l2.type = 'line'
      l1.stack = l2.stack = '总量'
      l1.data = arr[i]
      l2.data = arr[i + 3]
      line1.push(l1)
      line2.push(l2)
    }
    console.log(line1)
    commit(types.SET_SNAP_SHOOT_LINE, {line1, line2, lineY, name1: resArr[0].base_asset, name2: resArr[0].quote_asset})
  })
}

export const setRobotId = function ({commit}, id) {
  commit(types.SET_ROBOT_ID, id)
}
export const setModelList = function ({commit}) {
  return ModelList().then((res) => {
    if (res.data.code && res.data.code !== 0) {
      this.$message.error(this.$t('message.tip-29'))
    } else {
      commit(types.SET_MODEL_LIST, res.data.data)
      return res.data.data
    }
  }).catch(() => {
    this.$message.error(this.$t('message.tip-29'))
  })
}
